﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class test_updateuser1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["sourceUrl"] = Context.Request.UrlReferrer;	 //读取来源URL
            DetailsView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
            //GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
            //下面这行是自动换行
            //GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
            //DataBrid1();
            DataBrid();
        }
    }

    private void DataBrid()
    {
         DropDownList ddl = (DropDownList)this.DetailsView1.FindControl("jigou");
        int id = int.Parse(Request.QueryString["id"].ToString());
        string sqlText = "select max(id) as id,[jigou] from account group by [jigou]";
        string sqlText2 = "select * from account where id = @id";
        SqlParameter[] param = { new SqlParameter("@id",SqlDbType.Int)};
        param[0].Value = id;       
        SqlDataReader dr1 = DbHelper.ExecuteReader(CommandType.Text,sqlText2,param);
        if (dr1.Read())
        {
            ddl.Items.Insert(0, new ListItem(dr1["jigou"].ToString()));
        }    
        SqlDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sqlText, null);
        while (dr.Read())
        {
            ddl.Items.Add(new ListItem(dr["jigou"].ToString(), dr["id"].ToString()));
        }
        

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {

            Response.Redirect("UserBrow.aspx");


    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string username = ((TextBox)this.DetailsView1.FindControl("txt_user")).Text.Trim();
        string txt_role = ((TextBox)this.DetailsView1.FindControl("txt_role")).Text.Trim();
        string txt_mob = ((TextBox)this.DetailsView1.FindControl("txt_mob")).Text.Trim();
        string txt_mail = ((TextBox)this.DetailsView1.FindControl("txt_mail")).Text.Trim();
        string txt_gzdw = ((TextBox)this.DetailsView1.FindControl("txt_gzdw")).Text.Trim();
        string jigou = ((DropDownList)this.DetailsView1.FindControl("jigou")).SelectedItem.Text;
        string txt_grjj = ((TextBox)this.DetailsView1.FindControl("txt_grjj")).Text.Trim();
        string txt_leader = ((TextBox)this.DetailsView1.FindControl("txt_leader")).Text.Trim();
        string txt_city = ((TextBox)this.DetailsView1.FindControl("txt_city")).Text.Trim();
        string sqlText = "UPDATE [account] SET [username1] = @username1, [mobile] = @mobile, [mail] = @mail, [gzdw]=@gzdw, [jigou]=@jigou,[grjj]=@grjj,[leader]=@leader,[city]=@city,[xtsj2]=getdate(),[role] = @role WHERE [id] = @id";
        SqlParameter[] param = {new SqlParameter("@username1",SqlDbType.NVarChar,100),
                                new SqlParameter("@mobile",SqlDbType.VarChar,20),
                                new SqlParameter("@mail",SqlDbType.NVarChar,50),
                                new SqlParameter("@gzdw",SqlDbType.NVarChar,50),
                                new SqlParameter("@jigou",SqlDbType.NVarChar,100),
                                new SqlParameter("@grjj",SqlDbType.NText),
                                new SqlParameter("@leader",SqlDbType.NVarChar,20),
                                new SqlParameter("@city",SqlDbType.NVarChar,40),
                                new SqlParameter("@xtsj2",SqlDbType.DateTime),
                                new SqlParameter("@role",SqlDbType.NVarChar,20),
                                new SqlParameter("@id",SqlDbType.Int)};
        param[0].Value = username;
        param[1].Value = txt_mob;
        param[2].Value = txt_mail;
        param[3].Value = txt_gzdw;
        param[4].Value = jigou;
        param[5].Value = txt_grjj;
        param[6].Value = txt_leader;
        param[7].Value = txt_city;
        param[8].Value = DateTime.Now;
        param[9].Value = txt_role;
        param[10].Value = int.Parse(Request.QueryString["id"].ToString());
        int id = Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, sqlText, param).ToString());
        if (id > 0)
        {
            Response.Write("<script>alert('修改成功');location.href='UserBrow.aspx'</script>");
        }
        else
        {
            Response.Write("<script>alert('修改失败');location.href='updateuser1.aspx'</script>");
        }
    }
}
